﻿<%@ Application Language="C#" %>

<script runat="server">
    void Application_Start(object sender, EventArgs e) 
    {
        string totalVisits = System.IO.File.ReadAllText(Server.MapPath("~/log/" + "counter.txt"));
        //string todayFile = Server.MapPath("~/log/" + "counter_" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt");

        //if (!System.IO.File.Exists(todayFile))
        //{
        //    System.IO.File.WriteAllText(todayFile, "0");
        //}

        //string todayVisits = System.IO.File.ReadAllText(todayFile);

        if (string.IsNullOrEmpty(totalVisits)) totalVisits = "0";
        //if (string.IsNullOrEmpty(todayVisits)) todayVisits = "0";

        int totalVisit = int.Parse(totalVisits);
        //int todayVisit = int.Parse(todayVisits);
        
        Application["TotalVisit"] = totalVisit;
        //Application["TodayVisit"] = todayVisit;

        if (Application["TotalOnline"] == null)
        {
            Application["TotalOnline"] = 1;
        }
        else
        {
            Application["TotalOnline"] = (int)Application["TotalOnline"] + 1;
        }

        //HttpCookie cookie = new HttpCookie("ViewProduct", "");

        //cookie.Expires = DateTime.Now.AddDays(90);
        //Response.Cookies.Add(cookie);                
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //Response.Cookies["ViewProduct"].Expires = DateTime.Now.AddDays(-1);        
    }
        
    void Application_Error(object sender, EventArgs e) 
    {
        
    }

    void Session_Start(object sender, EventArgs e) 
    {
        Application.Lock();
        
        Application["TotalVisit"] = (int)Application["TotalVisit"] + 1;
        //Application["TodayVisit"] = (int)Application["TodayVisit"] + 1;
        Application["TotalOnline"] = (int)Application["TotalOnline"] + 1;

        System.IO.File.WriteAllText(Server.MapPath("~/log/") + "counter.txt", Application["TotalVisit"].ToString());
        //System.IO.File.WriteAllText(Server.MapPath("~/log/" + "counter_" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt"), Application["TodayVisit"].ToString());
                
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e) 
    {
        Application["TotalOnline"] = (int)Application["TotalOnline"] - 1;
    }
       
</script>
